package cn.edu.ujn.finallab.controller;


import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RequestMethod;



import cn.edu.ujn.finallab.dao.Customer;
import cn.edu.ujn.finallab.dao.Orders;
import cn.edu.ujn.finallab.service.ICustomerService;
import cn.edu.ujn.finallab.service.IOrderService;
import cn.edu.ujn.finallab.util.MD5Encoder;
import cn.edu.ujn.finallab.util.Page;

@Controller
public class CustomerController {

	// 创建用户service对象
	@Autowired
	private ICustomerService customerService;
	
	@Autowired
	private IOrderService orderService;

	// 查询用户
	@RequestMapping("/findCustomer")
	public String findById(int id, Model model) {
		Customer customer = this.customerService.findById(id);
		model.addAttribute("customer", customer);
		System.out.println(customer.toString());
		return "customer";
	}
	
	// 用户登录
	@RequestMapping("/customerLogin")
	public String customerLogin(String name, String password, Model model, HttpSession session) {
		String msg;
		System.out.println(password);
		Customer customer = this.customerService.selectByName(name);
		// MD5加密
		String result = "";
		MD5Encoder pe = new MD5Encoder();
		try {
			result = pe.geMd5(name, password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if(customer == null) {
			msg = "用户不存在";
			model.addAttribute("msg", msg);
			return "forward:/index.jsp";
		} else if(!result.equals(this.customerService.selectByName(name).getPassword())) {
			msg = "密码错误！";
			model.addAttribute("msg", msg);
			return "forward:/index.jsp";
		} else {
			session.setAttribute("user", this.customerService.selectByName(name));
			return "bookRoom";
		}	
			
	}
	
	// 用户注册
	@RequestMapping("/submitRegister")
	public String submitRegister(String name, String password, String phone, String idcard, Model model) {
		// 判断用户是否存在
		Customer cus = this.customerService.selectByName(name);
		if(cus != null) {
			model.addAttribute("msg", "用户名已存在！");
			return "register";
		}		
		// 判断身份证号是否重复
		cus = this.customerService.selectByIdcard(idcard);
		if(cus != null) {
			model.addAttribute("msg", "身份证号信息重复请重新输入身份证号！");
			return "register";
		}

		int id = 0;
		//MD5加密
		String result = "";
		MD5Encoder pe = new MD5Encoder();
		try {
			result = pe.geMd5(name, password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		Customer customer = new Customer(id, name, result, phone, idcard);
		int insert = this.customerService.insertCustomer(customer);
		if(insert > 0) {
			model.addAttribute("msg", "注册成功！请登录！");
			System.out.println("插入成功！");
			return "forward:/index.jsp";
		} else {
			System.out.println("插入失败！");
			return "register";
		}
//		System.out.println(insert);
//		return "register";
		
	}
	


	// 返回注册界面
	@RequestMapping("/register")
	public String register() {
		return "register";
	}
	
	// 返回登录界面
	@RequestMapping("/index")
	public String index() {
		return "redirect:/index.jsp";
	}
	
	// 返回个人中心界面
	@RequestMapping("/customerPersonalCenter")
	public String center() {
		
		return "customerPersonalCenter";
	}
	


	// 查询酒店全部顾客并跳转到顾客管理界面，使用分页查询
	@RequestMapping("/allCustomer")
	public String allCustomer(@RequestParam(defaultValue = "1") int currentPage, Model model) {
		Page<Customer> page = customerService.selectByPage(currentPage);
		model.addAttribute("pagemsg", page);// 设置一个属性
		return "customerManager";
	}
	
	// 删除顾客
	@RequestMapping("/deleteCustomer")
	public String deleteCustomer(int id) {
		int deleteCustomer = customerService.deleteCustomer(id);
		return "redirect:allCustomer";
	}
	
	// 根据顾客id查询当前顾客信息，并显示到前端供管理员修改
	// 还需要传过来当前的currentPage
	@RequestMapping("/modifyCustomer")
	public String modifyCustomer(int id, int currentPage, Model model) {
		Customer customer = this.customerService.findById(id);
		String idcard = customer.getIdcard();
		String showIdcard = idcard.substring(0, 6) + "********" + idcard.substring(14, 18);
		customer.setIdcard(showIdcard);
		model.addAttribute("customer", customer);
		model.addAttribute("currentPage", currentPage);
		return "modifyCustomer";
	}
	
	// 管理员提交修改后的顾客信息
	@RequestMapping("/modifycustomer")
	public String modifycustomer(int id, int currentPage, String name, String password, String phone, Model model) {
		// 先获取该id的顾客，再修改相应的信息，修改完成后保存到数据库
		Customer customer = this.customerService.findById(id);
		if(!"********".equals(password)) {
			String result = "";
			MD5Encoder pe = new MD5Encoder();
			try {
				result = pe.geMd5(name, password);
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			customer.setPassword(result);
		}	
		customer.setName(name);
		customer.setPhone(phone);
		int modifyCustomer = this.customerService.modifyCustomer(customer);
		// 修改成功后应该返回到分页查询页面，还需要传过来currentPage
		Page<Customer> page = customerService.selectByPage(currentPage);
		model.addAttribute("pagemsg", page);// 设置一个属性
		return "customerManager";
	}
	
	// 用户退出登录
	@RequestMapping("/customerLogout")
	public String customerLogout(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/index.jsp";
	}
	
	// 修改用户信息
	@RequestMapping("/modifyCustomerr")
	public String modifyCutomerr(String name, String password, String phone, String idcard, HttpSession session, Model model) {
		Customer customer = (Customer)session.getAttribute("user");
		if(!"******".equals(password)) {
			// MD5加密
			String result="";
			MD5Encoder pe = new MD5Encoder();
			try {
				result = pe.geMd5(name, password);
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			customer.setName(name);
			customer.setPassword(result);
			customer.setPhone(phone);
			customer.setIdcard(idcard);
			int modifyCustomerr = this.customerService.modifyCustomer(customer);
			if(modifyCustomerr > 0) {
				model.addAttribute("msg", "个人信息修改成功，请重新登录！");
				session.setAttribute("user", customer);
				return "forward:/index.jsp";
			}
		} else if(customer.getName().equals(name) && customer.getPhone().equals(phone) && customer.getIdcard().equals(idcard)) {
			model.addAttribute("msg", "没有修改的信息!");
			return "customerPersonalCenter";
		} else {
			customer.setName(name);
			customer.setPassword(customer.getPassword());
			customer.setPhone(phone);
			customer.setIdcard(idcard);
			int modifyCustomerr = this.customerService.modifyCustomer(customer);
			if(modifyCustomerr > 0) {
				model.addAttribute("msg", "个人信息修改成功!");
				session.setAttribute("user", customer);
				return "customerPersonalCenter";
			}
		}
		return "customerPersonalCenter";
	}
	
	// 注销用户信息
	@RequestMapping("/deleteCustomerr")
	public String deleteCustomerr(HttpSession session, Model model) {
		Customer customer = (Customer) session.getAttribute("user");
		int deleteCustomer = customerService.deleteCustomer(customer.getId());
		session.removeAttribute("user");
		model.addAttribute("msg", "注销成功！");
		return "redirect:/index.jsp";
	}
	
	//根据顾客身份证号进行查询
	@RequestMapping("/selectCustomerByIdcard")
	public String selectCustomerByIdcard(String customeridcard, Model model) {
		Customer customer = customerService.selectByIdcard(customeridcard);
		Page<Customer> page = new Page<Customer>();
		// 判断用户是否为空
		if(customer != null) {
			ArrayList<Customer> rows = new ArrayList<Customer>();
			rows.add(customer);
			page.setRows(rows);
			page.setPage(1);
			page.setTotalpage(1);
			page.setTotal(1);
		}
		model.addAttribute("pagemsg", page);// 设置一个属性
		return "customerManager";
	}
	
	@RequestMapping("/selectOrderr")
	public String selectByOrderNamee(HttpSession session, Model model) {
		
		Customer cus = (Customer) session.getAttribute("user");
		List<Orders> orders = this.customerService.selectByCustomerName(cus.getName());
		System.out.println(orders.toString());
		model.addAttribute("orderss", orders);
		return "customerOrderList";
	}
}

